package com.lindroy.androidplayer.ui.search

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseActivity
import com.lindroy.androidplayer.base.eventViewModel
import com.lindroy.androidplayer.extensions.initColor
import com.lindroy.androidplayer.extensions.loadListData
import com.lindroy.androidplayer.extensions.setLineDivider
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.bean.CollectBus
import com.lindroy.androidplayer.ui.adapter.HomeDataAdapter
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.androidplayer.viewmodel.CollectViewModel
import com.lindroy.androidplayer.viewmodel.SearchViewModel
import com.lindroy.lib_utils.extensions.launchActivity
import com.lindroy.lib_utils.extensions.shortToast
import com.youngfeng.snake.annotations.EnableDragToClose
import kotlinx.android.synthetic.main.activity_search_result.*

/**
 * @author Lin
 * @date 2021/5/2
 * @function 搜索结果页面
 * @Description
 */
private const val KEY_KEYWORD = "keyword"

@EnableDragToClose
class SearchResultActivity(override val layoutId: Int = R.layout.activity_search_result) :
    BaseActivity() {

    private val adapter by lazy { HomeDataAdapter() }
    private val searchViewModel: SearchViewModel by viewModels()
    private val collectViewModel: CollectViewModel by viewModels()

    override fun initView() {
        val keyword = intent.getStringExtra(KEY_KEYWORD) ?: ""
        searchViewModel.keyword = keyword
        initToolBar(keyword)
        refreshHeader.initColor()
        rvResult.setLineDivider()
        rvResult.adapter = adapter
    }

    override fun initData() {
        addLoadingObserve(collectViewModel)
        stateLayout.showLoading()
        searchViewModel.reqSearchResults(true)
    }

    override fun initListener() {
        refreshLayout.setOnRefreshListener {
            reqSearchResults(true)
        }
        refreshLayout.setOnLoadMoreListener {
            reqSearchResults(false, false)
        }
        searchViewModel.searchResultState.observe(this) {
            loadListData(it, adapter, refreshLayout, stateLayout)
        }
        collectViewModel.collectState.observe(this) {
            it.success.yes {
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect, it.originId)
            }
        }
        eventViewModel.collectEvent.observeInActivity(this) { collectBus ->
            val index = adapter.data.indexOfFirst {
                it.id == collectBus.id
            }
            if (index >= 0) {
                adapter.data[index].collect = collectBus.collect
                adapter.notifyItemChanged(index)
            }
        }
    }

    override fun initClick() {
        adapter.setOnItemClickListener { _, view, position ->
            val item = adapter.data[position]
            ContentActivity.start(
                mContext,
                item.id,
                item.link,
                Html.fromHtml(item.title).toString(),
                item.collect
            )
        }
        adapter.setOnCollectClickListener { collect, id ->
            collectViewModel.toggleCollectListArticle(collect,id)
        }
        stateLayout.setOnViewsClickListener { state, view ->
            reqSearchResults(true)
        }
    }

    private fun reqSearchResults(showLoading: Boolean, isRefresh: Boolean = true) {
        if (showLoading) {
            stateLayout.showLoading()
        }
        searchViewModel.reqSearchResults(isRefresh)
    }

    companion object {
        fun start(context: Context, keyword: String) {
            context.launchActivity<SearchResultActivity>(KEY_KEYWORD to keyword)
        }
    }
}